<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		#carousel {
			width: 1000px;
			height: 500px;
			background: #ccc;
			overflow: hidden;
			position: relative;
		}

		.imgs {
			width: 5000px;
			height: 500px;
			position: absolute;
			left: 0;
			transition: all .5s linear;
		}

		.a-img-box {
			width: 1000px;
			height: 500px;
			float: left;
		}
		.a-img-box img {
			width: 100%;
			height: 100%;
		}
	</style>
</head>
<body>
	<div id="carousel">
		<div class="imgs">
			<div class="a-img-box">				
				<img class="a-img" src="img/3.jpg" alt="">
			</div>
			<div class="a-img-box">
				<img class="a-img" src="img/1.jpg" alt="">
			</div>
			<div class="a-img-box">
				<img class="a-img" src="img/2.jpg" alt="">
			</div>
			<div class="a-img-box">				
				<img class="a-img" src="img/3.jpg" alt="">
			</div>
			<div class="a-img-box">				
				<img class="a-img" src="img/1.jpg" alt="">
			</div>
		</div>
	</div>
	<button onclick="preImg()">上一个</button>
	<button onclick="nextImg()">下一个</button>

	<script>
		let imgs = document.querySelector('.imgs')
		let aImg = document.querySelectorAll('.a-img')
		let offset = 1000

		imgs.style.left = `-${offset}px`

		function preImg() {
			imgs.style.left = `-${offset}px`
			offset -= 1000
			console.log(offset)
		}

		//记得加个节流，过渡完成才能再点
		function nextImg() {
			offset += 1000
			if(offset > (aImg.length-1)*1000) {
					//关掉过渡
					imgs.style.transition = 'all 0s linear'
					//拉到开始
					offset = 1000
					imgs.style.left = `-${offset}px`
					imgs.offsetTop		//强制重绘
					//打开过渡
					imgs.style.transition = 'all .5s linear'
					offset += 1000
			}
			imgs.style.left = `-${offset}px`
		}

		setInterval(nextImg, 4000)
 	</script>
</body>
</html>